package com.lxyk.yygh.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxyk.yygh.enums.RefundStatusEnum;
import com.lxyk.yygh.model.order.PaymentInfo;
import com.lxyk.yygh.model.order.RefundInfo;
import com.lxyk.yygh.order.mapper.RefundInfoMapper;
import com.lxyk.yygh.order.service.RefundInfoService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo>  implements RefundInfoService {

    /**
     * 保存退款记录
     * 保存之前查询是否有记录
     * @param paymentInfo
     */
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {

        //判断是否有重复数据添加
        QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", paymentInfo.getOrderId());
        queryWrapper.eq("payment_type", paymentInfo.getPaymentType());
        RefundInfo refundInfo = baseMapper.selectOne(queryWrapper);

        //有相同数据
        if(null != refundInfo) return refundInfo;
        refundInfo = new RefundInfo();
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setTradeNo(paymentInfo.getTradeNo());//交易编号
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());//金额
        refundInfo.setUpdateTime(new Date());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());//退款中 1 已退款 2
        baseMapper.insert(refundInfo);

        return refundInfo;
    }
}
